package com.sidemenu.demo

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.sidemenu.demo.databinding.FragmentChatGroupMenuBinding

private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"


class ChatGroupMenuFrag : Fragment() {

    private lateinit var mBinding: FragmentChatGroupMenuBinding

    private var param1: String? = null
    private var param2: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mBinding = FragmentChatGroupMenuBinding.inflate(inflater, container, false)
        //return inflater.inflate(R.layout.fragment_chat_group_menu, container, false)
        return mBinding.root
    }

    companion object {

        @JvmStatic
        fun newInstance() = ChatGroupMenuFrag()

        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            ChatGroupMenuFrag().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }

        private const val DURATION_SWITCH = 300L

        fun dpToPx(context: Context, dp: Float): Float {
            return (dp * context.resources.displayMetrics.density)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.vSwitch.setOnClickListener {
            onClickSwitch(mBinding.ivSwitch, mBinding.clMenuRoot)
        }
        mBinding.ivStore.setOnClickListener {
            Toast.makeText(context, "ivStore", Toast.LENGTH_SHORT).show()
        }
        mBinding.ivBoard.setOnClickListener {
            Toast.makeText(context, "ivBoard", Toast.LENGTH_SHORT).show()
            ChatGroupBoardActivity.jumpTo(requireContext())
        }
        mBinding.ivMachine.setOnClickListener {
            Toast.makeText(context, "ivMachine", Toast.LENGTH_SHORT).show()
        }
    }

    private fun onClickSwitch(ivSwitch: ImageView, clMenuRoot: View) {
        //Toast.makeText(context, "ivSwitch",Toast.LENGTH_SHORT).show()
//        if (isMenuOpen) {
//            mBinding.ivSwitch.setImageResource(R.mipmap.ic_group_menu_switch_off)
//        } else {
//            mBinding.ivSwitch.setImageResource(R.mipmap.ic_group_menu_switch_on)
//        }

        ivSwitch.animate().cancel()

        if (isMenuOpen) {
            ivSwitch.animate()
                .rotation(180F)
                .translationX(dpToPx(requireContext(), 3F))
                .setInterpolator(LinearInterpolator())
                .setDuration(DURATION_SWITCH)
                .start()

            clMenuRoot.animate()
                .translationX(dpToPx(requireContext(), 36F))
                .setInterpolator(LinearInterpolator())
                .setDuration(DURATION_SWITCH)
                .start()
        } else {
            ivSwitch.animate()
                .rotation(0F)
                .translationX(0F)
                .setInterpolator(LinearInterpolator())
                .setDuration(DURATION_SWITCH)
                .start()

            clMenuRoot.animate()
                .translationX(0F)
                .setInterpolator(LinearInterpolator())
                .setDuration(DURATION_SWITCH)
                .start()
        }
        isMenuOpen = !isMenuOpen
    }

    private var isMenuOpen = true

}